package com.qiguliuxing.dts.merchant.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.domain.DtsRole;
import com.qiguliuxing.dts.db.domain.DtsRoleExample;
import com.qiguliuxing.dts.service.base.DtsRoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商户端角色服务类
 * @author qiguliuxing
 * @since 1.0.0
 */
@Service
public class MerchantRoleService extends DtsRoleService {

    public List<DtsRole> querySelective(Integer brandId, String roleName, Integer page, Integer size, String sort, String order) {
        DtsRoleExample example = new DtsRoleExample();
        DtsRoleExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(roleName)) {
            criteria.andNameEqualTo("%" + roleName + "%");
        }
        // 过滤商户数据
        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        return roleMapper.selectByExample(example);
    }

    /**
     * 查询指定商户的角色
     * @param brandId
     * @return
     */
    public List<DtsRole> queryMerchantRole(Integer brandId) {
        DtsRoleExample example = new DtsRoleExample();
        example.or().andBrandIdEqualTo(brandId).andDeletedEqualTo(false);
        return roleMapper.selectByExample(example);
    }

    /**
     * 判断商户下是否已经存在某个角色
     * @param brandId
     * @param name
     * @return
     */
    public boolean checkExist(Integer brandId, String name) {
        DtsRoleExample example = new DtsRoleExample();
        example.or().andBrandIdEqualTo(brandId).andNameEqualTo(name).andDeletedEqualTo(false);
        return roleMapper.countByExample(example) != 0;
    }

}
